package com.Order;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.Result.BaseRespones;
import com.Result.StatusCode;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class VerifyState {
	/**
	 * @apiDefine Order 订单
	 */
	/**
	 * @apiDefine StudentConfirmOrderErrorMsg 用户确认订单错误响应信息
	 * @apiError 5041 无需确认
	 */
	
	/**
	 * @apiDefine StudentConfirmOrderSuccessMsg 用户确认订单成功响应信息
	 * @apiSuccess {int} code 响应码
	 * @apiSuccess {String} msg 响应信息
	 * @apiSuccess {timestamp} timestamp 时间戳
	 */

	/**
	 * @api{Post} /Order?method=StudentConfirm 确认订单
	 * @apiName StudentConfirm
	 * @apiGroup Order
	 * @apiDescription 学生确认订单信息  订单状态发生变化
	 *
	 * @apiParamExample {json} 确认订单样例
	 * {
	 * "StuID":10004,
	 *"OrderID":10006
	 * }
	 * 
	 * @apiUse StudentConfirmOrderErrorMsg
	 * @apiUse StudentConfirmOrderSuccessMsg
	 * 
	 * @apiSuccessExample {json} 学生确认成功
	 * {
	 * 'code':5040,
	 * 'msg':'学生确认成功',
	 * 'timestamp':'2019-05-21 17:39:09'
	 * }
	 */

	public String VerifyOrder(String requestString) throws IOException {
		String resource = "mybatisConfig.xml";
		InputStream is = org.apache.ibatis.io.Resources.getResourceAsStream(resource);
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = sessionFactory.openSession();
		// 返回对象声明
		BaseRespones response = null;
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		
		Order order=null;
		try {
			order=gson.fromJson(requestString, Order.class);
		} catch (Exception e) {
			response = new BaseRespones(StatusCode.jsonDeserializeFail);
			return gson.toJson(response);
		}
		if (order==null) {
			response = new BaseRespones(StatusCode.jsonDeserializeFail);
			return gson.toJson(response);
		}
		order.setOrderStatus(1);
		order.setConfirmTime(new Timestamp(new Date().getTime()));
		OrderMapper orderMapper=session.getMapper(OrderMapper.class);
		System.out.println(order);
		int rs=orderMapper.StuConfirm(order);
		if(rs!=1) {
			List<Order> orders=orderMapper.getOrders(order);
			if (orders.isEmpty()) {
				response =new BaseRespones(StatusCode.studentOrderConfirmFailStatus);
			}
			else {
				response =new BaseRespones(StatusCode.studentOrderConfirmFailStatus);
			}
		}
		else {
				session.commit();
				response=new BaseRespones(StatusCode.studentConfirmSuccess);
			}
		session.close();
		return gson.toJson(response);
	}
	/**
	 * @apiDefine citizenWithdrawOrderErrorMsg 撤回订单错误响应信息
	 * 
	 */
	
	/**
	 * @apiDefine citizenWithdrawOrderSuccessMsg 撤回订单成功响应信息
	 * @apiSuccess {int} code 响应码
	 * @apiSuccess {String} msg 响应信息
	 * @apiSuccess {timestamp} timestamp 时间戳
	 */

	/**
	 * @api{Delete} /Order?method=StudentWithdraw 取消订单
	 * @apiName StudentWithdraw
	 * @apiGroup Order
	 * @apiDescription 取消订单 恢复库存
	 *
	 * @apiParamExample {json} 取消订单样例
	 * {
	 * "StuID":10004,
	 *"OrderID":10006
	 * }
	 * 
	 * @apiUse citizenWithdrawOrderErrorMsg
	 * @apiUse citizenWithdrawOrderSuccessMsg
	 * 
	 * @apiSuccessExample {json} 取消成功
	 * {
	 * 'code':5050,
	 * 'msg':'撤销订单成功',
	 * 'timestamp':'2019-05-21 17:39:09'
	 * }
	 */

	public String studentWithdraw(String requestString) throws IOException {
		String resource = "mybatisConfig.xml";
		InputStream is = org.apache.ibatis.io.Resources.getResourceAsStream(resource);
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = sessionFactory.openSession();
		// 返回对象声明
		BaseRespones response = null;
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		
		Order order=null;
		OrderMapper orderMapper = (OrderMapper) session.getMapper(OrderMapper.class);
		try {
			order=gson.fromJson(requestString, Order.class);
		} catch (Exception e) {
			response = new BaseRespones(StatusCode.jsonDeserializeFail);
			return gson.toJson(response);
		}
		if (order==null) {
			response = new BaseRespones(StatusCode.jsonDeserializeFail);
			return gson.toJson(response);
		}
		int rs=orderMapper.deleteOrderbyID(order.getOrderID());
		if (rs!=1) {
			session.rollback();
			response = new BaseRespones(StatusCode.studentWithdrawFail);
			return gson.toJson(response);
		}
		
		session.commit();
		session.close();
		response = new BaseRespones(StatusCode.studentWithdrawSuccess);
		return gson.toJson(response);
	}
	/**
	 * @apiDefine studentPayedFail 用户付款失败响应信息
	 * 
	 */
	
	/**
	 * @apiDefine studentPayedSuccess 用户付款成功响应信息
	 * @apiSuccess {int} code 响应码
	 * @apiSuccess {String} msg 响应信息
	 * @apiSuccess {timestamp} timestamp 时间戳
	 */

	/**
	 * @api{Post} /Order?method=StudentPay 订单付款
	 * @apiName StudentPay
	 * @apiGroup Order
	 * @apiDescription 学生付款后  订单状态发生变化
	 *
	 * @apiParamExample {json} 付款订单样例
	 * {
	 * "StuID":10004,
	 * "OrderID":10006
	 * }
	 * 
	 * @apiUse studentPayedFail
	 * @apiUse studentPayedSuccess
	 * 
	 * @apiSuccessExample {json} 付款成功
	 * {
	 * 'code':5060,
	 * 'msg':'付款成功',
	 * 'timestamp':'2019-05-21 17:39:09'
	 * }
	 * 
	 *  @apiErrorExample {json} 付款成功
	 *  {
	 *   'code':5061,
	 *   'msg':'付款失败',
	 *   'timestamp':'2019-06-12 00:35:29'
	 *  }
	 */
	public String Payed(String requestString) throws IOException {
		String resource = "mybatisConfig.xml";
		InputStream is = org.apache.ibatis.io.Resources.getResourceAsStream(resource);
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = sessionFactory.openSession();
		// 返回对象声明
		BaseRespones response = null;
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		
		Order order=null;
		try {
			order=gson.fromJson(requestString, Order.class);
		} catch (Exception e) {
			response = new BaseRespones(StatusCode.jsonDeserializeFail);
			return gson.toJson(response);
		}
		if (order==null) {
			response = new BaseRespones(StatusCode.jsonDeserializeFail);
			return gson.toJson(response);
		}
		order.setOrderStatus(2);
		order.setConfirmTime(new Timestamp(new Date().getTime()));
		
		OrderMapper orderMapper=session.getMapper(OrderMapper.class);
		int rs=orderMapper.StuPay(order);
		System.out.println(rs);
		if(rs!=1) {
			List<Order> orders=orderMapper.getOrders(order);
			if (orders.isEmpty()) {
				response =new BaseRespones(StatusCode.studentPayedFail);
			}
			else {
				response =new BaseRespones(StatusCode.studentPayedFail);
			}
		}else {
				session.commit();
				response=new BaseRespones(StatusCode.studentPayedSuccess);
			}
		session.close();
		return gson.toJson(response);
	}

}
